package com.lfs.market.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lfs.market.constants.UserConstant;
import com.lfs.market.exception.ServiceException;
import com.lfs.market.model.R;
import com.lfs.market.model.dto.UserAdminPageDto;
import com.lfs.market.model.entity.User;
import com.lfs.market.model.enums.ResultCode;
import com.lfs.market.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/admin/web/user")
@SaCheckLogin
@SaCheckRole(value = {"admin"})
public class UserAdminController {
    @Autowired
    private UserService userService;

    @GetMapping("/page")
    public R<Page<User>> getUserList(UserAdminPageDto dto) {

        Page<User> page = userService.getUserList(dto);
        return R.ok(page);
    }

    @PutMapping("/able/{id}")
    public R ableUser(@PathVariable("id") String userId) {
        User user = userService.getById(userId);
        if (BeanUtil.isEmpty(user)) throw new ServiceException(ResultCode.NotFindError);
        user.setStatus(UserConstant.CHECK_COMPLETE);
        user.setUpdateTime(new Date());
        boolean update = userService.updateById(user);
        if (!update) throw new ServiceException(ResultCode.UpdateError);
        return R.ok();
    }

    @PutMapping("/disable/{id}")
    public R disableUser(@PathVariable("id") String userId) {
        User user = userService.getById(userId);
        if (BeanUtil.isEmpty(user)) throw new ServiceException(ResultCode.NotFindError);
        user.setStatus(UserConstant.CHECK_WAIT);
        user.setUpdateTime(new Date());
        boolean update = userService.updateById(user);
        if (!update) throw new ServiceException(ResultCode.UpdateError);
        StpUtil.logout(userId);
        return R.ok();
    }

    @PutMapping("/check/success/{id}")
    public R success(@PathVariable("id") String userId) {
        User user = userService.getById(userId);
        if (BeanUtil.isEmpty(user)) throw new ServiceException(ResultCode.NotFindError);
        if (StrUtil.isNotEmpty(user.getCheckAvatar())) user.setAvatar(user.getCheckAvatar());
        if (StrUtil.isNotEmpty(user.getCheckNickName())) user.setNickName(user.getCheckNickName());
        if (StrUtil.isNotEmpty(user.getCheckIntro())) user.setIntro(user.getCheckIntro());
        user.setCheckAvatar("");
        user.setCheckNickName("");
        user.setCheckIntro("");
        user.setUpdateTime(new Date());
        user.setCheckStatus(UserConstant.CHECK_COMPLETE);
        boolean update = userService.updateById(user);
        if (!update) throw new ServiceException(ResultCode.UpdateError);
        return R.ok();
    }

    @PutMapping("/check/fail/{id}")
    public R fail(@PathVariable("id") String userId) {
        User user = userService.getById(userId);
        user.setUpdateTime(new Date());
        user.setCheckStatus(UserConstant.CHECK_FAIL);
        boolean update = userService.updateById(user);
        if (!update) throw new ServiceException(ResultCode.UpdateError);
        return R.ok();
    }
}
